package com.ngocvo.QuanLyThuChi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class StartActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_layout);        
        
        CashFlowData CFData = new CashFlowData(StartActivity.this);
        
        final Button btn_Thu = (Button)findViewById(R.id.btn_Thu);
        final Button btn_Chi = (Button)findViewById(R.id.btn_Chi);
        final Button btn_Vay = (Button)findViewById(R.id.btn_ChoVay);
        final Button btn_No  = (Button)findViewById(R.id.btn_MuonNo);
        final TextView total_Thu = (TextView)findViewById(R.id.total_thu);
        final TextView total_Chi = (TextView)findViewById(R.id.total_chi);
        final TextView total_Vay = (TextView)findViewById(R.id.total_vay);
        final TextView total_No = (TextView)findViewById(R.id.total_no);
        final TextView balance = (TextView)findViewById(R.id.balance);                        
        
        final TextView[] total = new TextView[]{total_Thu, total_Chi, total_Vay, total_No};
        
        //Set value for totals
        for(int i = 0; i<total.length; i++){
            try{
            	total[i].setText(Long.toString(CFData.getTotal(i)));
            }catch (Exception e) {
    			AlertDialog.Builder builder = new AlertDialog.Builder(StartActivity.this);
    			builder.setTitle("Error orrcurs!");
    			builder.setMessage("The error is: " + e.getMessage());
    			builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
    				
    				@Override
    				public void onClick(DialogInterface arg0, int arg1) {
    					// TODO Auto-generated method stub								
    				}
    			});
    			builder.show();
    		}
        }
        
        //Set value for balance
        int intBalance = (Integer.parseInt(total_Thu.getText().toString()) + Integer.parseInt(total_Vay.getText().toString())
        		 - (Integer.parseInt(total_Chi.getText().toString()) + Integer.parseInt(total_No.getText().toString())));
        
        balance.setText(Integer.toString(intBalance));
        
        //Event listener for btn_Thu
        btn_Thu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(StartActivity.this, ThuActivity.class);
				startActivity(i);				
			}
		});
        
        //Event listener for btn Chi
        btn_Chi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(StartActivity.this, ChiActivity.class);
				startActivity(i);
			}
		});
        
        //Event listener for btn_Vay
        btn_Vay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(StartActivity.this, VayActivity.class);
				startActivity(i);				
			}
		});
        
        //Event listener for btn_No
        btn_No.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(StartActivity.this, NoActivity.class);
				startActivity(i);
			}
		});
    
        //Event listener for total_thu
        total_Thu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//Create bundle to add to intent
				Bundle b = new Bundle();
				b.putInt("flowType", FlowType.Thu.type());
				b.putString("title", getResources().getString(R.string.lbl_thongkeThu));
				//Create intent to send
				Intent i = new Intent(StartActivity.this, ThongKeActivity.class);
				i.putExtras(b);
				startActivity(i);
			}
		});
    
        //Event listener for total_chi
        total_Chi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Bundle b = new Bundle();
				b.putInt("flowType", FlowType.Chi.type());
				b.putString("title", getResources().getString(R.string.lbl_thongkeChi));
				
				Intent i = new Intent(StartActivity.this, ThongKeActivity.class);
				i.putExtras(b);
				startActivity(i);				
			}
		});
   
        //Event listener for total_vay
        total_Vay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Bundle b = new Bundle();
				b.putInt("flowType", FlowType.ChoVay.type());
				b.putString("title", getResources().getString(R.string.lbl_thongkeVay));
				
				Intent i = new Intent(StartActivity.this, ThongKeActivity.class);
				i.putExtras(b);
				startActivity(i);				
			}
		});
    
        //Event listener for total_no
        total_No.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Bundle b = new Bundle();
				b.putInt("flowType", FlowType.No.type());
				b.putString("title", getResources().getString(R.string.lbl_thongkeNo));
				
				Intent i = new Intent(StartActivity.this, ThongKeActivity.class);
				i.putExtras(b);
				startActivity(i);				
			}
		});
    }
}